﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class StoreBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public StoreBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string ProductDetailID, string FloorID)
        {
            if (DAO.T_STOREs.Where(t => t.PRODUCT_DETAIL_ID.ToString() == ProductDetailID 
                                    && t.FloorID.ToString() == FloorID).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_STORE> GetStoreList()
        {
            var list = DAO.T_STOREs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_STORE GetStoreByBarCode(string BarCode)
        {
            return DAO.T_STOREs.Where(t => t.T_PRODUCT_DETAIL.BAR_CODE == BarCode).SingleOrDefault();
        }

        public T_STORE GetStoreByProductDefaultId(string ProductDefaultID)
        {
            return DAO.T_STOREs.Where(t => t.PRODUCT_DETAIL_ID.ToString() == ProductDefaultID).SingleOrDefault();
        }

        public T_STORE GetStoreByID(string ID)
        {
            return DAO.T_STOREs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddStore(T_STORE Store)
        {
            try
            {
                Store.ID = DAO.GetGUID();
                DAO.T_STOREs.InsertOnSubmit(Store);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateStore(T_STORE Store)
        {
            try
            {
                T_STORE updateEntity = DAO.T_STOREs.SingleOrDefault(t=>t.ID.ToString() == Store.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.FloorID = Store.FloorID;
                updateEntity.GROSS_WEIGHT = Store.GROSS_WEIGHT;
                updateEntity.NET_WEIGHT = Store.NET_WEIGHT;
                updateEntity.PACKET_QTY = Store.PACKET_QTY;
                updateEntity.PACKET_QTY2 = Store.PACKET_QTY2;
                updateEntity.PRODUCT_DETAIL_ID = Store.PRODUCT_DETAIL_ID;
                updateEntity.PRODUCT_ID = Store.PRODUCT_ID;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteStore(string ID)
        {
            try
            {
                T_STORE deleteEntity = DAO.T_STOREs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_STOREs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
